package com.zqc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqc.common.Const;
import com.zqc.dto.SetmealDto;
import com.zqc.entity.Setmeal;
import com.zqc.entity.SetmealDish;
import com.zqc.exception.CustomException;
import com.zqc.mapper.SetMealMapper;
import com.zqc.service.SetMealDishService;
import com.zqc.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
    @Autowired
    public SetMealDishService setMealDishService;

    /**
     * 添加套餐，操作setmeal表和setmealdish表
     *
     * @param setmealDto
     */
    @Override
    public void saveSetmealAndDish(SetmealDto setmealDto) {
        // 向setmeal表插入数据
        this.save(setmealDto);
        // 向setmealdish表插入数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        // 保存套餐和菜品的关联信息
        setMealDishService.saveBatch(setmealDishes);
    }


    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    public void removeSetMealAndDish(Long[] ids) {
        // 判断该套餐是否停售，如果是起售状态不能删除
        long count = this.count(new LambdaQueryWrapper<Setmeal>()
                .in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, Const.SALE_ON)
        );

        if (count > 0) {
            throw new CustomException("套餐正在起售，不能删除");
        }
        // 删除setmeal表中的数据
        this.removeBatchByIds(Arrays.asList(ids));

        // 删除setmealdish表中的数据
        setMealDishService.remove(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId,ids));
    }

    /**
     * 根据id查询套餐详细信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetMealInfo(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        // 查询setmeal表获取基本信息
        Setmeal setmeal = this.getById(id);
        // 对象拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);

        // 查询套餐中有的菜品
        List<SetmealDish> setmealDishes = setMealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));

        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    /**
     * 更新套餐需要更新setMeal表和setmealdish表中的数据
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateStemealAndDish(SetmealDto setmealDto) {
        // 更新setmeal表
        this.updateById(setmealDto);

        // 删除setmealdish表中的数据
        setMealDishService.remove(new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getSetmealId,setmealDto.getId()));

        // 新菜品数据插入setmealdish表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> setmealDishList = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setMealDishService.saveBatch(setmealDishList);
    }


}
